﻿// Author: Radu-Mihail
// Project: UDPClient, File: Client.cs
// Namespace: UDPClient, Class: Client
// Path: G:\Secure Project\WebCrawlerProject\UDPClient
// Code lines: 197, Size of file: 5.61 KB
// Creation date: 1/11/2009 7:08 PM
// Last modified: 1/11/2009 7:43 PM


#region Using directives
using Error;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UDPServer.CommandNamespace;
using UDPServer.DataNamespace;
#endregion

namespace UDPClient
{
    /// <summary>
    /// Client
    /// </summary>
  
    public class Client:IDisposable
    {
        #region Private Fields
        /// <summary>
        /// Client socket
        /// </summary>
        public Socket clientSocket;

        /// <summary>
        /// Ep server
        /// </summary>
        public EndPoint epServer;

        /// <summary>
        /// String name
        /// </summary>
        public string strName;

        /// <summary>
        /// Byte data
        /// </summary>
        public byte[] byteData = new byte[1024];

        /// <summary>
        /// Is connected
        /// </summary>
        private bool _isConnected = false;
        #endregion
        /// <summary>
        /// _is connected
        /// </summary>
        /// <returns>Bool</returns>
        public bool ConnectionStatus { 
            get  
            {
                return _isConnected;
            } 
        } // _isConnected

        /// <summary>
        /// Create client
        /// </summary>
        public Client()
        {          
           
        } // Client(name, ip)

        /// <summary>
        /// Login
        /// </summary>
        /// <param name="name">Name</param>
        /// <param name="ipAddress">Ip address</param>
        public bool Login(string name, string ip)
        {
            strName = name;
            try
            {
                //Using UDP sockets
                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                //IP address of the server machine
                IPAddress ipAddress = IPAddress.Parse(ip);
                //Server is listening on port 1000
                IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 1000);

                epServer = (EndPoint)ipEndPoint;

                Data msgToSend = new Data();
                msgToSend.cmdCommand = Command.Login;
                msgToSend.strMessage = null;
                msgToSend.strName = strName;

                byte[] byteData = msgToSend.ToByte();
                //Login to the server
                clientSocket.SendTo(byteData, 0, byteData.Length,  SocketFlags.None, epServer);
                byteData = new byte[1024];
                clientSocket.ReceiveFrom(byteData, ref epServer);
                Data receivedMessage = new Data(byteData);
                if (receivedMessage.strMessage.Equals("OK"))
                {
                    _isConnected = true;
                    return true;
                } // if (receivedMessage.strMessage.Equals)
                return false;
              
            } // try
            catch (Exception ex)
            {
                ErrorManagement.ManageException(ex);
                return false;
            } // catch
        } // Login(name, ip)


        /// <summary>
        /// Ask for seed link
        /// </summary>
        /// <returns>String</returns>
        public string AskForSeedLink() 
        {
            try
            {
                if (_isConnected)
                {
                    Data msgToSend = new Data();
                    msgToSend.strName = strName;
                    msgToSend.cmdCommand = Command.Seed;
                    msgToSend.strMessage = "";

                    byte[] byteData = msgToSend.ToByte();

                    clientSocket.SendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer);

                    byteData = new byte[1024];
                    clientSocket.ReceiveFrom(byteData, ref epServer);

                    Data receivedData = new Data(byteData);

                    if (receivedData.strMessage.Contains("http://"))
                    {
                        return receivedData.strMessage;
                    } // if (receivedData.strMessage.Contains)
                    return null;
                }
                return null;// if (_isConnected)
            }
            catch (Exception exc)
            {
                ErrorManagement.ManageException(exc);
                return null;
            }// try
            
        } // AskForSeedLink()
   

        /// <summary>
        /// Close client
        /// </summary>
        private void CloseClient()
        {
            try
            {
                //Send a message to logout of the server
                Data msgToSend = new Data();
                msgToSend.cmdCommand = Command.Logout;
                msgToSend.strName = strName;
                msgToSend.strMessage = null;

                byte[] b = msgToSend.ToByte();
                clientSocket.SendTo(b, 0, b.Length, SocketFlags.None, epServer);
                clientSocket.Close();
            } // try
            catch (Exception ex)
            {
                ErrorManagement.ManageException(ex);
            } // catch
        } // CloseClient()



        #region IDisposable Members

        public void Dispose()
        {
            if(_isConnected)
                CloseClient();
        }

        #endregion
    } // class Client
} // namespace UDPClient
